<template>
  <div >
<!--搜索框-->
    <el-form :inline="true" ref="searchForm" :model="keyword" class="demo-form-inline">
      <el-form-item>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        <el-button type="danger" icon="el-icon-delete" @click="delBatchMeasure" size="small">批量删除</el-button>
      </el-form-item>
      <el-form-item label="员工ID" prop="wWid">
        <el-input size="small" clearable v-model="keyword.wWid" placeholder="员工ID"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="wName">
        <el-input size="small" clearable v-model="keyword.wName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary" size="small " icon="el-icon-search" @click="search">查询</el-button>
        <el-button type="primary" plain size="small " icon="el-icon-search" @click="resetSearchForm('searchForm')">重置</el-button>
        <el-button type="success" plain size="small " icon="el-icon-download" @click="dialogExcelVisible = true">导入</el-button>
        <el-button type="success" plain size="small " icon="el-icon-upload2" @click="downloadExcel">导出</el-button>
      </el-form-item>
    </el-form>

     <!--表单数据-->
    <el-table :data="workerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="员工ID" align="center" prop="wWid" />
      <el-table-column label="姓名" align="center" prop="wName" />
      <el-table-column label="所属部门" align="center" prop="wType" />
      <el-table-column label="所属角色" align="center" prop="wRole" />
      <el-table-column label="员工状态" align="center" prop="wState"
                       :filters="[{ text: '启用', value: '启用' }, { text: '请假', value: '请假' }]"
                       :filter-method="filterwState"
                       filter-placement="bottom-end">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.wState === '启用' ? 'primary' : 'danger'"
            disable-transitions>{{scope.row.wState}}</el-tag>
        </template>
        </el-table-column>
      <el-table-column label="创建人" align="center" prop="wCreateBy" />
      <el-table-column label="创建时间" align="center" prop="wCreateTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改单位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form  :model="workerManageList" :rules="workerManageLists" ref="workerManageListsForm" label-width="130px">
        <el-form-item label="员工ID" prop="wWid">
          <el-input v-model="workerManageList.wWid" autocomplete="off" placeholder="请输入员工ID" />
        </el-form-item>
        <el-form-item label="姓名" prop="wName">
          <el-input v-model="workerManageList.wName" autocomplete="off" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="所属部门" prop="wType">
          <el-input v-model="workerManageList.wType" type="textarea" placeholder="请输入所属部门" />
        </el-form-item>
        <el-form-item label="所属角色" prop="wRole">
          <el-input v-model="workerManageList.wRole" type="textarea" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="员工状态" prop="wState">
          <el-input v-model="workerManageList.wState" type="textarea" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="带班人姓名" prop="wCreateBy">
          <el-input v-model="workerManageList.wCreateBy" type="textarea" placeholder="请输入带班人姓名" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('workerManageListsForm')" >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 上传excel的对话框 -->
    <el-dialog title="上传员工信息Excel" :visible.sync="dialogExcelVisible" width="40%">
      <el-upload
        class="upload-demo"
        drag
        action="http://localhost:8888/worker/upload/excel"
        accept=".xlsx,.xls"
        :on-success="uploadExcelSuccess"
        :on-error="uploadExcelError"
        multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传.xlsx,.xls文件，且不超过500kb</div>
      </el-upload>
    </el-dialog>

  </div>
</template>

<script>
import * as echarts from 'echarts'
import {getList,addworker,updateworker,del,delBatch,getNum} from "@/api/set_worker";
import {title} from "@/settings";


export default {
name:"WorkerManage",
  data() {
    return {
      //回显页面分页信息数据
      workerList:[],
      pageNum:1,
      pageSize:5,
      currentPage:1,
      total:0,
      //模糊搜索关键字
      keyword:{
        wWid:null,
        wName:null,
      },
      title:"",
      open:false,
      //对话框表单内容
      workerManageList:{
        wWid:null,
        wName:null,
        wType:null,
        wRole:null,
        wState:null,
        wCreateBy:null,

      },
      //对话框表单校验
      workerManageLists: {
        wWid: [
          { required: true, message: "员工ID不能为空", trigger: "blur" }
        ],
       wName: [
          { required: true, message: "员工姓名不能为空", trigger: "blur" }
        ],
       wType: [
          { required: true, message: "所属部门不能为空", trigger: "blur" }
        ],
       wRole: [
          { required: true, message: "职位不能为空", trigger: "blur" }
        ],
        wState: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        wCreateBy: [
          { required: true, message: "带班人不能为空", trigger: "blur" }
        ],
      },
      //批量删除序号
      workerdelList:[],
      //序号取出
      arr:[],
      //取出被删除人名字
      arrwName:[],

      dialogExcelVisible:false,

    }
  },
  methods: {
  //查全部
    fetchData(){ // 获取数据

      // 组装查询参数
      let params = {
        pageNum:this.pageNum,
        pageSize:this.pageSize,
        wWid:this.keyword.wWid,
        wName:this.keyword.wName,
      }
      // getList是api/unit.js中自己
      // 查全部
      getList(params).then(res => {
        let pageInfo = res.data;
        // console.log("pageInfo",pageInfo)
        // this.loading = false;
        this.workerList = pageInfo.list;
        // console.log( this.workerList)
        this.total=pageInfo.total
        // this.initChart()
      })
    },
    //更改页面大小
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData(this.pageNum, val)
    },
    //设置当前页
    handleCurrentChange(val) {
      this.pageNum = val;
      this.fetchData(val, this.pageSize)
    },
    //w_state的显示
    filterwState(value, row) {
      return row.wState === value;
    },
    //新增员工信息
    handleAdd(){
      this.workerManageList = {}
    this.open = true;
    this.title = '添加员工信息';
    },

    /** 提交按钮 */
    submitForm(formName) {
      // console.log("formName--->"+formName)
      this.$refs[formName].validate((valid )=> {
        if (valid) {
          if(this.title == '添加员工信息'){
            //在此处添加
            // console.log(this.unitMeasure)
            addworker(this.workerManageList).then(res => {
              this.$message({
                type: 'success',
                message: '添加成功',
                duration: 1000
              });
              // console.log(res)
              this.fetchData();
            })
          }else{
            console.log("===================",this.workerManageList);
            updateworker(this.workerManageList).then(res=>{
              this.$message({
                type: 'success',
                message: '更新成功',
                duration: 1000
              });
              this.fetchData();
            })
          }
        }else{
          return false;
        }

        this.open = false;
      });
    },
    //取消新增
    cancel(){
      this.open = false;
      this.workerManageList = {}
    },
    //模糊搜索
    search(){
    this.fetchData()
    },
    //重置
    resetSearchForm(formName){
      this.$refs[formName].resetFields();
      this.fetchData()
    },
    //单个删除
    handleDelete(row){
      // console.log(row)
      del({wId:row.wId}).then(res=>{
        this.$message({
          type: 'success',
          message: '删除成功',
          duration: 1000
        });
        this.fetchData()
      })
    },
    //修改
    handleUpdate(row){
      this.title = "修改员工信息";
      this.open = true;
      this.workerManageList = row
      // console.log("=================",this.workerManageList)

    },
    // 多选框选中数据
    handleSelectionChange(val) {
      this.arr = []
      this.arrwName = []
      this.workerdelList = val
      console.log(this.workerdelList)
      for (let i = 0; i < this.workerdelList.length; i++) {
        let user = this.workerdelList[i]
        this.arrwName.push(user.wName)
        this.arr.push(user.wId)
      }
    },
    //批量删除
    delBatchMeasure(){
      this.$confirm('是否确认删除用户编号为"['+this.arrwName+']"的数据项？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(res=>{
        const idString = this.arr.join(",");
        // console.log(idString)
        var params={
          'ids':idString
        }
        delBatch(params).then(res=>{
          this.$message({
            type: 'success',
            message: '删除成功',
            duration: 1000
          });
          this.fetchData()
        })
    })
    },
    //文件上传
    uploadExcelSuccess(){
      this.$message({
        type:"success",
        message:"上传成功"
      })
      this.dialogExcelVisible = false;
      this.fetchData();
    },
    uploadExcelError(err){
      this.$message({
        type:"error",
        message:err
      })
    },
    //文件下载
    downloadExcel(){
      this.$confirm('确定要导出数据吗？', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        window.location.href = "http://localhost:8888/worker/download/excel"
      }).catch(()=>{
        this.$message({
          type:"info",
          message:"已取消导出数据"
        })
      })
    }
  },
  created() {
    this.fetchData()
  },
  //初始化Echarts
  // mounted() {
  //   this.initChart()
  // },
}
</script>

